package net.wanho.service.impl;

import net.wanho.po.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-07-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

   /**
    *  用户表 按查询分页查询
    */
    @Override
    public IPage<User> findPage(int pageNum, int pageSize,User user) {
        //验证
        if(pageNum<1)
            pageNum=1;
        if(pageSize<1)
            pageSize=10;
        if(ObjectUtils.isEmpty(user))
            user = new User();

        // 查询条件
        QueryWrapper<User> wrapper = getUserQueryWrapper(user);
        IPage<User> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

   /**
    *  用户表 查询条件
    */
     private QueryWrapper<User> getUserQueryWrapper(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        // 编号
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getId()),User::getId, user.getId());
        // 用户名
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getUsername()),User::getUsername, user.getUsername());
        // 密码
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getPassword()),User::getPassword, user.getPassword());
        // 是否删除（0表示正常，1表示删除）
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getIsDeleted()),User::getIsDeleted, user.getIsDeleted());
        // 创建时间
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getCreateTime()),User::getCreateTime, user.getCreateTime());
        // 更新时间
        wrapper.lambda().eq(!ObjectUtils.isEmpty(user.getUpdateTime()),User::getUpdateTime, user.getUpdateTime());
        return wrapper;
    }
}
